<template>
  <div class="container">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-button type="primary" size="small" @click="goFile()"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <input
        type="file"
        style="display: none;"
        id="file"
        ref="files"
        accept=".ttf"
        @change="upload"
      />
      <el-divider direction="horizontal" content-position="left"></el-divider>
      <el-table v-loading="loading" :data="List" border stripe>
        <el-table-column
          align="center"
          label="名称"
          prop="name"
        ></el-table-column>
        <el-table-column align="center" label="链接">
          <template slot-scope="{ row }">
            {{ decodeURI(row.url) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button type="danger" size="mini" plain @click="showDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { getFont, updateFont, addFont, delFont } from "@/api/font";
export default {
  components: { Pagination },
  data() {
    return {
      List: [],
      total: 0,
      add_vis: false,
      loading: false,
      listQuery: {
        page: 1,
        limit: 10
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goFile() {
      this.$refs.files.click();
    },
    upload(e) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let file = e.target.files[0];
      let data = new FormData();
      data.set("url", file);
      data.set("name", file.name);
      addFont(data).then(res => {
        this.$notify({
          title: "成功",
          message: "上传成功",
          type: "success"
        });
        this.getList();

        loading.close();
      });
    },
    showDelete(row) {
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        });
        delFont(row.id)
          .then(res => {
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success"
            });
            this.getList();
          })
          .finally(_ => {
            loading.close();
          });
      });
    },
    getList() {
      this.loading = true;
      getFont(this.listQuery).then(res => {
        this.List = res.results;
        this.total = res.count;
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.box {
  display: flex;
}
.box .el-collapse {
  flex: 1;
}

.action {
  height: 48px;
  display: flex;
  align-items: center;
}
.action .add {
  font-size: 20px;
  padding: 5px;
  color: #409eff;
  cursor: pointer;
}
.action .delete {
  font-size: 20px;
  padding: 5px;
  color: #f56c6c;
  cursor: pointer;
}
</style>
